<?php defined('SYSPATH') or die('No direct access allowed.');

class Model_Sysconfig extends ORM
{

    protected $_table_name = 'sysconfig';

    /*
     * 根据webid获取所有配置信息
     * */
    public function getConfig($webid)
    {
        $arr = $this->where('webid', '=', $webid)->get_all();
        $out = array();
        foreach ($arr as $row)
        {
            $out[$row['varname']] = $row['value'];
        }
        return $out;
    }

    /*
     * 根据webid保存配置信息
     * */
    public function saveConfig($arr)
    {

        $webid = ARR::get($arr, 'webid');

        foreach ($arr as $k => $v)
        {
            /* if(!get_magic_quotes_gpc())
             {
                 $v = addslashes($v);
             }
             else
             {
                 $v = $v;
             }*/
            if ($k !== 'webid')
            {
                $row = $this->where('webid', '=', $webid)->and_where('varname', '=', $k)->find();

                if (isset($row->id)) //如果存在则修改,如果不存在则创建
                {

                    //$v = $k=='cfg_tongjicode' ? addslashes($v) : $v;
                    $row->value = $v;
                    $row->update();
                }
                else
                {
                    $row->varname = $k;
                    $row->value = $v;
                    $row->webid = $webid;
                    $row->create();
                }

                $row->clear();
            }

        }
        return true;
    }

    /**
     * @function 获取系统配置
     * @param int $webid
     * @param null $fields 要获取的字段数组或字符
     * @param $isvalue 是否直接返回值
     * @return array
     */
    public static function get_configs($webid = 0, $fields = null, $isvalue = null)
    {

        $query = DB::select('value', 'varname')->from('sysconfig')->where('webid', '=', $webid);
        if (is_array($fields))
            $query->and_where('varname', 'in', $fields);
        if (is_string($fields))
            $query->and_where('varname', '=', $fields);
        $list = $query->execute()->as_array();
        $out = array();
        foreach ($list as $row) {
            if ($isvalue)
                return $row['value'];
            $out[$row['varname']] = $row['value'];
        }

        return $out;
    }
}